/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jvmlearn;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author hitdong
 */
public class FileClassLoader extends ClassLoader
{

    private String fileName;

    public String getFileName()
    {
        return fileName;
    }

    public void setFileName(String fileName)
    {
        this.fileName = fileName;
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException
    {
        byte[] b = loadClassData(fileName);
        return defineClass(name, b, 0, b.length);
    }

    private byte[] loadClassData(String fileName)
    {
        File file = new File(fileName);
        byte[] bytes = new byte[(int) file.length()];
        try
        {
            new FileInputStream(file).read(bytes);
        } catch (IOException ex)
        {
            Logger.getLogger(FileClassLoader.class.getName()).log(Level.SEVERE, null, ex);
        }
        return bytes;
    }
}
